<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/4
 * Time: 19:55
 */

namespace app\shared\ext;
class LkSms
{

    const ACCOUNT = 'CQJS007550';
    const PWD = 'bh@123';
    private static $url = 'https://sdk2.028lk.com/sdk2/BatchSend2.aspx?';
    private static $template = [
        'code' => '您的验证码为：',
    ];

    public static function valid_code(string $mobile)
    {
        $code = mt_rand(1000, 9999);
        return self::sendMsg($mobile, self::$template['code'] . $code . '。') > 0 ? $code : false;
    }

    private static function sendMsg($phone, $message)
    {
        $msg   = rawurlencode(mb_convert_encoding($message, "gb2312", "utf-8"));
        $query = [
            'CorpID'   => self::ACCOUNT,
            'Pwd'      => self::PWD,
            'Mobile'   => $phone,
            'Content'  => $msg,
            'Cell'     => '',
            'SendTime' => '',
        ];
        foreach ($query as $key => $val) self::$url .= $key . '=' . $val . '&';
        return file_get_contents(rtrim(self::$url, '&'));
    }
}